﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Doo.Caching;
using System.Web.UI;

namespace Doo.WebUI
{
    public class WebManager
    {
        DooPage page;
        HttpContext context;

        public ResourceManager ResourceManager
        {
            get
            {
                return page.ResourceManager;
            }
        }

        public WebManager(DooPage page)
        {
            this.page = page;
            this.context = HttpContext.Current;
            this.context.Items["Doo.WebUI.WebManager"] = this;
            if (page != null)
            {
                page.PreRenderComplete += new EventHandler(page_PreRenderComplete);
            }
        }

        /// <summary>
        /// 客户端控件ID集合
        /// </summary>
        HashSet<string> ClientControls = new HashSet<string>();

        public void RegisterClientControl(string clientid)
        {
            ClientControls.Add(clientid);
        }

        void page_PreRenderComplete(object sender, EventArgs e)
        {
            if (Cache != null) page.Header.Controls.Add(new LiteralControl("<script src='" + DataVersionHandler.GetUrl(PageCacheHelper.GetKeys(Cache.ClientKeys)) + "'></script>"));
            if (ClientControls.Any()) ScriptManager.RegisterStartupScript(page, page.GetType(), "__doo_control",
                "Doo._CreatePageControls(['" + string.Join("','", ClientControls.ToArray()) + "']);", true);
        }
        public static WebManager Current
        {
            get
            {
                return HttpContext.Current.Items["Doo.WebUI.WebManager"] as WebManager;
            }
        }

        public ContentCache Cache
        {
            get { return ContentCache.Current; }
        }

        public bool EnableCache
        {
            get { return Cache.EnableCache; }
            set { Cache.EnableCache = value; }
        }

        /// <summary>
        /// 添加缓存键
        /// </summary>
        /// <param name="serverkeys">一组键，每个键的格式为ka+kb+kc</param>
        /// <param name="clientkeys">一组键，每个键的格式为ka+kb+kc</param>
        public void AddCacheKeys(string[] serverkeys, string[] clientkeys)
        {
            if (serverkeys != null)
                foreach (string k in serverkeys)
                {
                    Cache.AddServerCacheKey(k.Split('+'));
                }
            if (clientkeys != null)
                foreach (string k in clientkeys)
                {
                    Cache.AddClientCacheKey(k.Split('+'));
                }
        }

        public void AddCacheKeys(string serverkeys, string clientkeys)
        {
            string[] sks = null, cks = null;
            if (!string.IsNullOrEmpty(serverkeys)) sks = serverkeys.Split(',');
            if (!string.IsNullOrEmpty(clientkeys)) cks = clientkeys.Split(',');
            AddCacheKeys(sks, cks);

        }

        /// <summary>
        /// 向流中输出跨域回调
        /// </summary>
        /// <param name="context"></param>
        /// <param name="paras">参数</param>
        public static void WriteCrossAjaxResponse(HttpContext context, params object[] paras)
        {
            string parastr = string.Join("|", paras.Select(r => r.ToString()).ToArray()),
                proxyhost = context.Request.QueryString["host"],
                methodname = context.Request.QueryString["callbackname"];
            string html = string.Format("<html><head></head><body><iframe src='http://{0}/proxy.htm#{1}|{2}'></iframe></body></html>", proxyhost, methodname, parastr);
            context.Response.Write(html);
        }
    }
}
